package net.ikenway.springboot.demo.Config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sun.jdi.LongValue;
import net.ikenway.springboot.demo.Dao.*;
import net.ikenway.springboot.demo.Model.Entity.*;
import net.ikenway.springboot.demo.Utils.HibernateProxyTypeAdapter;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.security.core.token.Sha512DigestUtils;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;


/**
 * @author MasterKenway <zk@ikenway.net>
 * @Description
 * @date Created Date 12/9/2020
 */
@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
public class SpringDataJPAConfig {


    @Bean
    @ConditionalOnMissingBean
    public Gson gson(GsonBuilder gsonBuilder) {
        return gsonBuilder.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY).create();
    }

    // 为数据库插入初始数据
    @Autowired
    public void initDatabase(UserRepository userRepository,
                             OrderRepository orderRepository,
                             ProductSortRepository productSortRepository,
                             ProductRepository productRepository,
                             AddressRepository addressRepository,
                             ActivityRepository activityRepository) {
        User user = userRepository.findById(Long.valueOf("1")).orElse(null);
        if (user == null) {
            user = new User("username@qq.com", Sha512DigestUtils.shaHex("password"));
            user.setUserId(Long.valueOf("1"));
            user.setIdentity(User.Identity.ROOT);
            userRepository.save(user);
        }
        Address address = new Address(Long.valueOf("1"),"人名", "134565689", "广东省", "东莞市", "松山湖", "详细地址", user);
        List<Address> addressList = new ArrayList<>();
        addressList.add(address);
        user.setAddress(addressList);
        userRepository.save(user);

        try {
            ProductSort productSort = new ProductSort(Long.valueOf("11"), "奶茶11", 1, true, null);
            ProductSort productSort2 = new ProductSort(Long.valueOf("12"), "奶茶12", 2, true, null);
            ProductSort productSort3 = new ProductSort(Long.valueOf("13"), "奶茶13", 3, true, null);
            ProductSort productSort4 = new ProductSort(Long.valueOf("14"), "奶茶14", 4, true, null);
            productSort = productSortRepository.save(productSort);
            productSort2 = productSortRepository.save(productSort2);
            productSort3 = productSortRepository.save(productSort3);
            productSort4 = productSortRepository.save(productSort4);

            Product product = new Product(Long.valueOf("11"), "柠檬果茶", null, productSort, new BigDecimal(15), new BigDecimal(17), new BigDecimal(1),10, false, null);
            Product product2 = new Product(Long.valueOf("12"), "草莓酸奶1", null, productSort, new BigDecimal(16), new BigDecimal(17), new BigDecimal(1),20, false, null);
            Product product3 = new Product(Long.valueOf("13"), "草莓酸奶2", null, productSort, new BigDecimal(15), new BigDecimal(20), new BigDecimal(1),30, false, null);
            Product product4 = new Product(Long.valueOf("14"), "草莓酸奶3", null, productSort2, new BigDecimal(15), new BigDecimal(17), new BigDecimal(1),40, false, null);
            Product product5 = new Product(Long.valueOf("15"), "草莓酸奶4", null, productSort3, new BigDecimal(15), new BigDecimal(17), new BigDecimal(1),50, false, null);
            product = productRepository.save(product);
            product2 = productRepository.save(product2);
            product3 = productRepository.save(product3);
            product4 = productRepository.save(product4);
            product5 = productRepository.save(product5);

//            OrderSub orderSub = new OrderSub(Long.valueOf("1"), product, null, new BigDecimal("15"), OrderSub.Additions.None, OrderSub.Scale.MEDIUM, null);
//            List<OrderSub> orderSubs = new ArrayList<>();
//            orderSubs.add(orderSub);
//
//            orderSub = new OrderSub(Long.valueOf("2"), product, null, new BigDecimal("15"), OrderSub.Additions.None, OrderSub.Scale.MEDIUM, null);
//            orderSubs.add(orderSub);
//
//            Order order = new Order(Long.valueOf("1"), user, orderSubs, new BigDecimal("15"), new Timestamp(System.currentTimeMillis()), null, Order.Type.DINE, Order.Status.TOPAY, address);
//            orderRepository.save(order);

            List<Product> products = new ArrayList<>();
            products.add(product2);
            products.add(product3);
            Activity activity = new Activity(Long.valueOf("1"), "活动", "url",
                    new BigDecimal("0.8"), products, 1, true);
            activityRepository.save(activity);


        } catch (Exception exception) {
            exception.printStackTrace();
        }

    }
}
